/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.farsunset.lvxin.service.impl;

import com.farsunset.cim.sdk.server.model.Message;
import com.farsunset.lvxin.data.repository.MessageRepository;
import com.farsunset.lvxin.service.IDGenerateService;
import com.farsunset.lvxin.service.MessageService;
import com.farsunset.lvxin.util.MessageUtil;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

	@Resource
	private MessageRepository messageRepository;


	@Resource
	private IDGenerateService snowflakeService;
	

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void delete(Long id) {
		messageRepository.deleteById(id);
	}

	@Override
	public Message queryById(Long id) {
		return messageRepository.findOne(id);
	}


	@Override
	public List<Message> queryOffLineMessages(String receiver) {

		Message condition  = new Message();
		condition.setReceiver(receiver);
		condition.setState(Message.STATE_NOT_RECEIVED);
		ExampleMatcher matcher = ExampleMatcher.matching().withIgnorePaths("timestamp","id");

		return messageRepository.findAll(Example.of(condition,matcher) , Sort.by(Sort.Order.asc("timestamp")));
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void save(List<Message> dataList) {
		messageRepository.saveAll(dataList);
	}


	@Transactional(rollbackFor = Exception.class)
	@Override
	public void save(Message message) {

		message.setId(snowflakeService.getLongId());
		message.setState(Message.STATE_NOT_RECEIVED);
		message.setTimestamp(System.currentTimeMillis());

		messageRepository.saveAndFlush(message);

	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateState(Long id, String status) {
		messageRepository.updateState(id, status);
	}


	@Override
	public Page<Message> queryPage(Message model, Pageable pageable) {
		MessageUtil.cleanEmptyProperties(model);
		ExampleMatcher matcher = ExampleMatcher.matching().withIgnorePaths("timestamp","id");
		return this.messageRepository.findAll(Example.of(model,matcher), pageable);
	}


	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateBatchReceived(String account) {
		messageRepository.updateBatchReceived(account);
	}


}
